package com.qfedu.model;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author 王海龙
 * @date 2023-04-18
 */
public class Cart {

    private int cid;//唯一id
    private int id;//用户的id

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "cid=" + cid +
                ", id=" + id +
                ", items=" + items +
                ", totalCount=" + totalCount +
                ", totalPrice=" + totalPrice +
                '}';
    }

    private  Map<Integer,CartItem> items = new LinkedHashMap<>(); //linked有序
    //购物车的商品总数
    private int totalCount;
    //购物车商品的总价格
    private BigDecimal totalPrice;
    //get从 购物项中求出
    public int getTotalCount() {
        //遍历map集合求出总数量
        if (items.size()>0){
            Set<Integer> set = items.keySet();
            int count = 0;
            for (Integer integer : set) {
                int count1 = items.get(integer).getCount();
                count+=count1; //遍历求和
            }
            return  count;
        }

        return 0;
    }
    //get从 购物项中求出
    public BigDecimal getTotalPrice() {
        //遍历map集合求出总数量
        if (items.size()>0){
            Set<Integer> set = items.keySet();

            BigDecimal bigDecimal = new BigDecimal(0);
            for (Integer integer : set) {
                BigDecimal totalPrice = items.get(integer).getTotalPrice();
                bigDecimal= bigDecimal.add(totalPrice);
                System.out.println(bigDecimal.intValue());
            }
            return  bigDecimal;
        }

        return new BigDecimal(0);
    }

    public Map<Integer, CartItem> getItems() {
        return items;
    }

    public void setItems(Map<Integer, CartItem> items) {
        this.items = items;
    }

}
